﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using libtcodWrapper;
using Console = libtcodWrapper.Console;

namespace HDHack
{
    class UnEquipInterface
    {

        Console displayConsole;
        public bool IsActive = false;
        Dungeon theDungeon;
        Player p;
        List<BodyPart> equipedBodyParts;

        public UnEquipInterface(Player ip, Console dCon, Dungeon d)
        {
            p = ip;
            displayConsole = dCon;
            theDungeon = d;
        }



        int ConvertItemID(int charVal)
        {
            int retVal = charVal - 97;
            return retVal;
        }

        public void ProcessChoice(KeyPress k)
        {
            int choiceIndex = ConvertItemID(k.Character);
            if (Convert.ToInt16(k.Character) > 96 && choiceIndex < equipedBodyParts.Count)
            {
                equipedBodyParts[choiceIndex].EquipedItem.IsEquiped = false;
                p.UnEquipItem(equipedBodyParts[choiceIndex]);
                equipedBodyParts.Clear();
                displayConsole.Clear();
                IsActive = false;
                theDungeon.ComplexCommand = false;
            }
            else if (k.KeyCode == KeyCode.TCODK_ESCAPE)
            {
                displayConsole.Clear();
                IsActive = false;
                theDungeon.ComplexCommand = false;
            }
        }

        private List<BodyPart> GetEquipedBodyParts()
        {
            List<BodyPart> tbp = new List<BodyPart>();
            foreach (BodyPart bp in p.BodyParts)
            {
                if (bp.EquipedItem != null)
                    tbp.Add(bp);
            }
            return tbp;
        }


        public void DisplayEquipedItems()
        {


            displayConsole.Clear();
            int lastline = 1;
            int itemASCIIIndex = 97;
            equipedBodyParts = GetEquipedBodyParts();
            if (equipedBodyParts.Count > 0)
            {
                lastline = displayConsole.PrintLineRect("Equiped Items:", 1, 1, displayConsole.GetConsoleWidth(), displayConsole.GetConsoleHeight(), LineAlignment.Left);
                displayConsole.PrintLine("", 1, lastline, LineAlignment.Left);
                lastline++;
                lastline++;

                foreach (BodyPart bp in equipedBodyParts)
                {
                    string itemName = "empty";
                    if (bp.EquipedItem != null)
                        itemName = bp.EquipedItem.name;
                    lastline = lastline + displayConsole.PrintLineRect("(" + Convert.ToChar(itemASCIIIndex) + ") " + bp.name + "-" + itemName, 1, lastline, displayConsole.GetConsoleWidth(), displayConsole.GetConsoleHeight(), LineAlignment.Left);
                    displayConsole.PrintLine("", 1, lastline, LineAlignment.Left);
                    lastline++;

                }
                IsActive = true;
            }
            else
            {
                displayConsole.PrintLine("Nothing to unequip!", 1, lastline, LineAlignment.Left);
                theDungeon.ComplexCommand = false;
                IsActive = false;

            }
        }




    }
}
